//
//  NewFeatureViewController.swift
//  我的微博
//
//  Created by ZhangChao on 15/9/4.
//  Copyright © 2015年 ZhangChao. All rights reserved.
//

import UIKit

private let HMNewFeatureCellID = "HMNewFeatureCellID"
private let NewFeatureCount = 4
class NewFeatureViewController: UICollectionViewController {
    
    // 重写init方法
    init(){
        
        super.init(collectionViewLayout: UICollectionViewFlowLayout())
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // 注册可充用cell
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: HMNewFeatureCellID)

        prepareLayout()
        
        // 发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewControllerNotification, object: nil, userInfo: nil)
    }
    
    private func prepareLayout(){
        
        // 获取当前的布局属性
        let layout = collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.itemSize = view.bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
    }

    // MARK: UICollectionViewDataSource

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return NewFeatureCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(HMNewFeatureCellID, forIndexPath: indexPath) as! NewFeatureCell
    
        // Configure the cell
//        cell.backgroundColor = UIColor.redColor()
        
        cell.imageIndex = indexPath.item
        
        return cell
    }

    // 用collectionView的代理方法设置按钮的动画
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        // 获取当前显示的cell的indexPath
        let path = collectionView.indexPathsForVisibleItems().last!
        
        // 判断是不是最后一个
        if path.item == NewFeatureCount - 1{
            
            let cell = collectionView.cellForItemAtIndexPath(path) as! NewFeatureCell
            
            cell.showStartButton()
        }
    }
    
}
private class NewFeatureCell: UICollectionViewCell {
    // 懒加载属性
    private lazy var iconView = UIImageView()
    
    private lazy var startButton: UIButton = {
    
        let button = UIButton()
        
        button.setTitle("开始体验", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.addTarget(self, action: "beginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.sizeToFit()
        
        return button
        
    }()
    
    // 按钮点击事件
    
    @objc private func beginBtnClick(){
        
        // 发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewControllerNotification, object: nil)
    }
    
    // 图片索引属性
    var imageIndex: Int = 0{
        
        didSet{
            
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            
            startButton.hidden = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder:aDecoder)
        
        setupUI()
    }
    
    private func setupUI() {
        
        // 1.添加控件
        addSubview(iconView)
        addSubview(startButton)
        // 2.指定布局
        iconView.frame = bounds
        
        // 自动布局
        startButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
    }
    
    // 设置按钮的动画效果
    private func showStartButton(){
        
        startButton.hidden = false
        
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(4, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 4, options: [], animations: { () -> Void in
            
            self.startButton.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                
                
        }
    }
    
}
